package nl.biz_e.urenfact.client;

import nl.biz_e.aclclient.shared.AclAuthenticator;
import nl.biz_e.appengine.lib.shared.login.GoogleLoginInfo;
import nl.biz_e.gwt.baselib.ui.client.menu.IMenuItem;
import nl.biz_e.gwt.baselib.ui.client.menu.MenuItem;
import nl.biz_e.gwt.baselib.ui.client.menu.IMenuItem.MenuItemClickedListener;
import nl.biz_e.gwtappengine.lib.client.AbstractApplication;
import nl.biz_e.urenfact.client.ui.UrenFacturatiePanel;

/**
 * Main application class for the UrenFacturatie system.<br/>
 */
public class UrenFacturatie extends AbstractApplication {
    private IMenuItem mnuHome;
    private UrenFacturatiePanel homePanel;

    /**
     * Constructs a new UrenFacturatie instance
     * 
     */
    public UrenFacturatie() {
        super("Uren Registratie en Facturatie");
        this.setShowPreLoginPanel(false);
        setUseGoogleLogin(true);
        this.setAuthenticator(new AclAuthenticator());

        MenuItemClickedListener menuListener = new MenuItemClickedListener() {
            @Override
            public void menuItemClicked(IMenuItem item) {
                item.getManager().setOnlySelectedMenuItem(item);
                if (item == mnuHome) {
                    showPanel(getHomePanel());
                }
            }
        };

        mnuHome = new MenuItem("Home", menuListener, false);

        this.addMenuItem(mnuHome);
        mnuHome.setSelected(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onLoggedIn() {
        showPanel(getHomePanel());
    }

    private UrenFacturatiePanel getHomePanel() {
        if (homePanel == null) {
            GoogleLoginInfo info = this.getLoginInfo();
            String name = "";
            if (info != null) {
                name = info.getName();
            } else {
                name = "Unknown";
            }
            homePanel = new UrenFacturatiePanel(name);
        }
        return homePanel;
    }
}
